package com.taiyuan.zfmvp.controller.testThread;

import com.alibaba.fastjson.JSONArray;
import com.taiyuan.zfmvp.controller.Student;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @param request
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption: 重入锁等待测试
 * @Date 2022/3/8
 * @return
 */
public class TryLockTest {

    ReentrantLock lock = new ReentrantLock();
    public void m1(){
        try {
            if(lock.tryLock(5,TimeUnit.SECONDS)){
                System.out.println(Thread.currentThread().getId()+"获取了锁");
                TimeUnit.SECONDS.sleep(3);
            }else{
                System.out.println(Thread.currentThread().getId()+"等待超时");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(lock.isHeldByCurrentThread() && lock.isLocked()){
                lock.unlock();
            }
        }
    }


    public static void main(String[] args) {
        Student stu = null;
        if(null != stu){
            System.out.println("111");
        }
    }

    public  void newState() {

    }

}
